package com.wxj.edu.controller;


import com.wxj.commonutils.result.R;
import com.wxj.edu.entity.Chapter;
import com.wxj.edu.entity.chapter.ChapterVo;
import com.wxj.edu.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wxj
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/edu/chapter")
//@CrossOrigin
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 1 课程大纲列表，根据课程id进行查询
     */
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.success().data("allChapterVideo",list);
    }

    /**
     * 2 添加章节
     */
    @PostMapping("addChapter")
    public R addChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return R.success();
    }

    /**
     * 3 根据章节id查询
     */
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
        return R.success().data("chapter",chapter);
    }

    /**
     * 4 修改章节
     */
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.success();
    }

    /**
     * 4 删除
     */
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId) {
        boolean flag = chapterService.deleteChapter(chapterId);
        if (flag) {
            return R.success();
        } else {
            return R.error();
        }
    }

}

